﻿namespace LinqPractice;

class Program
{
    static void Main(string[] args)
    {
        int[] nums = new int[] { 3,5, 3453,33,2,935} ;
        /*var result = nums
            .Where(i => i > 10)
            .OrderByDescending(i=>i);
        foreach (var value in result)
        {
            Console.WriteLine(value);
        }
        
        var myresult = nums.MyWhere(v => v > 5);
        foreach (var element in myresult)
        {
            Console.WriteLine($"myExtensionWhere:{element}");
        }

        var result1 = MyYieldWhere(["1111","22","333333","453"], i => i.Length>2);
        foreach (var value in result1)
        {
            Console.WriteLine($"myWhere:{value}");
        }*/

        var result2 = nums.GroupBy(e => e);
        foreach (var group in result2)
        {
            Console.WriteLine(group.Key);
            foreach (var element in group)
            {
                Console.WriteLine(element);
            }

            Console.WriteLine("----------------------------------------------------");
        }
    }

    static IEnumerable<T> MyWhere<T>(IEnumerable<T> items,Func<T,bool> f)
    {
        List<T> result = new List<T>();
        foreach (var value in items)
        {
            if (f(value))
            {
                result.Add(value);
            }
        }
        return result;
    }

    static IEnumerable<T> MyYieldWhere<T>(IEnumerable<T> items, Func<T, bool> condition)
    {
        foreach (var value in items)
        {
            if (condition(value))
            {
                yield return value;
            }
        }
    }
}

public static class MyExtensionLinqWhere
{
    public static IEnumerable<T> MyWhere<T>(this IEnumerable<T> items, Func<T, bool> condition)
    {
        foreach (var value in items)
        {
            if (condition(value))
            {
                yield return value;
            }
        }
    }
}

